//
//  EastMarketTabBarController.m
//  EastMarket
//
//  Created by 李鹏程 on 16/10/12.
//  Copyright © 2016年 guoguo. All rights reserved.
//

#import "EastMarketTabBarController.h"

@interface EastMarketTabBarController ()

@end

@implementation EastMarketTabBarController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self setupUI];
}

- (void)setupUI {
    
    // 设置 TabBarItemTestAttributes 的颜色。
    [self setUpTabBarItemTextAttributes];
    
    // 设置子控制器
    [self setUpChildViewController];
    
    // 处理tabBar，使用自定义 tabBar 添加 发布按钮
    [self setUpTabBar];
    
    [[UITabBar appearance] setBackgroundImage:[self imageWithColor:[UIColor whiteColor]]];
    
    //去除 TabBar 自带的顶部阴影
    [[UITabBar appearance] setShadowImage:[[UIImage alloc] init]];
   
    
    //设置导航控制器颜色为
    [[UINavigationBar appearance] setBackgroundImage:[self imageWithColor:[UIColor colorWithRed:255.0 / 255 green:80.0 / 255 blue:0.0  / 255 alpha:1.0]] forBarMetrics:UIBarMetricsDefault];
    
    //设置导航栏的title颜色以及大小
    [[UINavigationBar appearance] setTitleTextAttributes:@{NSForegroundColorAttributeName: [UIColor whiteColor], NSFontAttributeName:[UIFont boldSystemFontOfSize:20.0f]}];
    
    //设置导航栏的状态栏前景色
    [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleLightContent];
}

#pragma mark -
#pragma mark - Private Methods

/**
 *  利用 KVC 把 系统的 tabBar 类型改为自定义类型。
 */
- (void)setUpTabBar{
    
    [self setValue:[[EastMarketTabBar alloc] init] forKey:@"tabBar"];
}

/**
 *  tabBarItem 的选中和不选中文字属性
 */
- (void)setUpTabBarItemTextAttributes{
    
    [[UITabBarItem appearance] setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:[UIColor blackColor], NSForegroundColorAttributeName, [UIFont fontWithName:@"Helvetica" size:12.0f],NSFontAttributeName,nil] forState:UIControlStateNormal];
    [[UITabBarItem appearance] setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:[UIColor colorWithRed:255.0 / 255 green:80.0 / 255 blue:0.0  / 255 alpha:1.0], NSForegroundColorAttributeName, [UIFont fontWithName:@"Helvetica" size:12.0f],NSFontAttributeName,nil] forState:UIControlStateSelected];
}

/**
 *  添加子控制器，我这里值添加了3个，没有占位自控制器
 "HomeViewController.h"
 "ProductViewController.
 "MaterialsViewControlle
 "ShoppingViewController
 "MineViewController.h"
 */
- (void)setUpChildViewController{
    
    [self addOneChildViewController:[[UINavigationController alloc]initWithRootViewController:[[HomeViewController alloc] init]]
                          WithTitle:@"首页"
                          imageName:@"home"
                  selectedImageName:@"home-active"];
    [self addOneChildViewController:[[UINavigationController alloc]initWithRootViewController:[[ProductViewController alloc]init]]
                          WithTitle:@"全部商品"
                          imageName:@"product"
                  selectedImageName:@"product-active"];
    
    [self addOneChildViewController:[[UINavigationController alloc] initWithRootViewController:[[MaterialsViewController alloc] init]]
                          WithTitle:@"常用料"
                          imageName:@"material"
                  selectedImageName:@"material-active"];
    
    [self addOneChildViewController:[[UINavigationController alloc]initWithRootViewController:[[ShoppingViewController alloc]init]]
                          WithTitle:@"购物车"
                          imageName:@"cart"
                  selectedImageName:@"cart-active"];
    [self addOneChildViewController:[[UINavigationController alloc]initWithRootViewController:[[MineViewController alloc]init]]
                          WithTitle:@"我的"
                          imageName:@"my"
                  selectedImageName:@"my-active"];
}

/**
 *  添加一个子控制器
 *
 *  @param viewController    控制器
 *  @param title             标题
 *  @param imageName         图片
 *  @param selectedImageName 选中图片
 */
- (void)addOneChildViewController:(UIViewController *)viewController WithTitle:(NSString *)title imageName:(NSString *)imageName selectedImageName:(NSString *)selectedImageName{
    
    viewController.view.backgroundColor = EastMarket_backgroundColor;
    viewController.tabBarItem.title = title;
    viewController.title = title;
    viewController.tabBarItem.image = [UIImage imageNamed:imageName];
    UIImage *image = [UIImage imageNamed:selectedImageName];
    image = [image imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    viewController.tabBarItem.selectedImage = image;
    [self addChildViewController:viewController];
}

//这个方法可以抽取到 UIImage 的分类中
- (UIImage *)imageWithColor:(UIColor *)color
{
    NSParameterAssert(color != nil);
    
    CGRect rect = CGRectMake(0, 0, 1, 1);
    // Create a 1 by 1 pixel context
    UIGraphicsBeginImageContextWithOptions(rect.size, NO, 0);
    [color setFill];
    UIRectFill(rect);   // Fill it with your color
    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    
    return image;
}


- (void)tabBar:(UITabBar *)tabBar didSelectItem:(UITabBarItem *)item {

    NSUInteger tabIndex = [tabBar.items indexOfObject:item];
    if (tabIndex == 1) {
        globalManager.strKeyName = nil;
    }
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
